<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .root {
            width: 416px;
            height: 416px;
            margin: 0 auto;
            background-color: rgb(120, 116, 144);
            overflow: hidden;
            position: relative;
        }
        
        #con {
            width: 100%;
            position: relative;
            top: -418px;
        }
        
        .cell {
            width: 100px;
            height: 100px;
            border: 2px solid rgb(54, 74, 129);
            background-color: #fff;
            float: left;
        }
        
        .black {
            width: 100px;
            height: 100px;
            background-color: rgb(0, 0, 0);
        }
        
        h2 {
            color: rgb(255, 0, 0);
        }
        
        #score {
            position: absolute;
            top: 0;
            right: 0;
        }
        
        .btn {
            width: 200px;
            height: 100px;
            text-align: center;
            /* background-color: rgb(0, 0, 0); */
            color: rgb(255, 255, 255);
            font-size: 30px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>

<body>
    <h2>分数</h2>
    <h2 id="score">0</h2>
    <div class="root">
        <div id="con">
        </div>
    </div>
    <div class=" btn"> 开始游戏</div>

</body>

</html>
<script>
    // 创建div
    function creatediv(divelement) {
        var div = document.createElement("div");
        div.className = divelement;
        return div;
    }

    function craeterow() {
        var con = document.querySelector('#con');
        var row = creatediv(row);
        var arr = creatcell(); //定义div cell的类名,其中一个为cell black
        con.appendChild(row);
        for (var i = 0; i < 4; i++) {
            row.appendChild(creatediv(arr[i]));
        }
        if (con.firstChild == null) {
            con.appendChild(row);
        } else {
            con.insertBefore(row.con.firstChild);
        }

    }
    // 删除最后一排;
    function delrow() {
        var con = document.querySelector('#con');
        if (con.childNodes == 6) {
            con.removeChild(con.lastChild);
        }

    }
    // 创建一个数组,
    function creatcell() {
        var temp = ["cell", "cell", "cell", "cell"];
        var ma = Math.floor(Math.random() * 4);
        temp[ma] = "cell black";
        return temp;
    }
    // 黑块动;
    function move() {
        var con = document.querySelector('#con');
        var top = parseInt(window.getComputedStyle(con, null)["top"]);
        if (speed + top > 0) {
            top = 0;
        } else {
            top += speed;
        }
        con.style.top = top + "px";
        over();
        if (top == 0) {
            craeterow();
            con.style.top = "-102px";
            delrow();
        }

    }
    // 加速函数
    function speedup() {
        speed += 2;
        if (speed == 20) {
            alert("那你很棒棒嘛");
        }
    }
    // 判断游戏是否结束
    function over() {
        var rows = con.childNodes;
        if (rows.length == 5 && rows[rows.length - 1].pass !== 1) {
            fail();

        }
        for (let i = 0; i < rows.length; i++) {
            if (rows[i].passl == 1) {
                fail();
            }
        }
    }
    // 游戏结束
    function fail() {
        clearInterval(clock);
        flag = false;
        confirm("你的最终得分为" + parseInt($("score").innerHTML));
        var con = document.querySelector("#con");
        con.innerHTML = "";
        document.querySelector("score").innerHTML = 0;
        con.style.top = "-408px";
    }
    // 判断是否点击白块;
    function judge(ev) {
        // if () {

        // }

    }


    var btn = document.querySelector(".btn");
    btn.onclick = function() {
        setInterval(function() {

            craeterow();
            creatcell()
            creatediv(divelement);
            move();
        }, 1000)
    };
</script>